package com.huiztech.maystall.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.huewu.pla.lib.extra.MultiColumnPullToRefreshListView;
import com.huewu.pla.lib.extra.MultiColumnPullToRefreshListView.OnRefreshListener;
import com.huewu.pla.lib.internal.PLA_AbsListView;
import com.huewu.pla.lib.internal.PLA_AbsListView.OnScrollListener;
import com.huiztech.maystall.adapter.WaterfallAdapter;
import com.huiztech.maystall.mvc.Controller;
import com.huiztech.maystall.mvc.MaystallView;

public class ProductListActivity extends BaseActivity implements OnRefreshListener, OnScrollListener {

    private MultiColumnPullToRefreshListView waterfallView;// 瀑布流组件
    private List<Map<String, Object>> productList;
    private WaterfallAdapter adapter;
    private boolean flag = true;
    private Controller controller;
    private MaystallView view;
    private Button backBtn;
    private TextView titleTextView;
    
    private Button newBtn , fieryBtn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        
        view = new MaystallView();
        controller = new Controller(view, this);
        productList = new ArrayList<Map<String, Object>>();
        
        titleTextView = (TextView) findViewById(R.id.product_list_title_text);
        titleTextView.setText(getIntent().getStringExtra("title").toString());
        
        backBtn = (Button) findViewById(R.id.product_list_back_btn);
        backBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        // 瀑布流
        waterfallView = (MultiColumnPullToRefreshListView) findViewById(R.id.list);
        productList = controller.getProductList(0);
        adapter = new WaterfallAdapter(productList, this);
        waterfallView.setAdapter(adapter);
        waterfallView.setOnRefreshListener(this);
        waterfallView.setOnScrollListener(this);
        
        adapter.notifyDataSetChanged();
        
        newBtn = (Button) findViewById(R.id.product_list_new_btn);
        fieryBtn = (Button) findViewById(R.id.product_list_fiery_btn);

        newBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                newBtn.setTextColor(getResources().getColorStateList(R.color.white)); // 最新按钮颜色变白
                newBtn.setBackgroundResource(R.drawable.home_newbtn_bg);// 最新按钮填充背景

                fieryBtn.setTextColor(getResources().getColorStateList(R.color.main_btn_color));// 最热按钮变色
                // 最热按钮背景透明
                fieryBtn.setBackgroundColor(R.color.touming);
                fieryBtn.setBackgroundDrawable(null);

                productList = controller.getNewProduct(0);
                adapter.notifyDataSetChanged();
            }
        });

        fieryBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                fieryBtn.setTextColor(getResources().getColorStateList(R.color.white));
                fieryBtn.setBackgroundResource(R.drawable.home_newbtn_bg);

                newBtn.setTextColor(getResources().getColorStateList(R.color.main_btn_color));
                newBtn.setBackgroundColor(R.color.touming);
                newBtn.setBackgroundDrawable(null);

                productList = controller.getFireyProduct(0);
                adapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onScrollStateChanged(PLA_AbsListView view, int scrollState) {
        
    }

    @Override
    public void onScroll(PLA_AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem == 0) {
            flag = true;
        }
    }

    @Override
    public void onRefresh() {
        // 刷新之后

        // 刷新之后调用
        waterfallView.onRefreshComplete();
    }
}
